<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Contact to Group</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<h4>Select Group to add contact to</h4>
<form action="#" method="post">
Select Group:<br/>
<select name="name">
<option value="N/A">Select a Group from Dropdown</option>
<?php 
$conn=mysql_connect("localhost","root","") or die("Could not Establish Connection".mysql_error());
$db=mysql_select_db("contacts_ms",$conn) or die("No DB Selected".mysql_error());
$st="EM";
$qry=mysql_query("SELECT * FROM info_group");
$cqry=mysql_query("SELECT * FROM info_contacts");
while($r=mysql_fetch_array($qry)){//This code selects the group that the user wants to associate contact with.
	?>
		<option value="<?php echo $r['name'] ?>"> <?php echo $r['name'] ?></option> <?php
}
?>
</select><br/>
Select a Contact:<br/>
<select name="phone">
<option value="n/a">Select a Contact's Name to add to Group</option>
<?php
while($rw=mysql_fetch_array($cqry)){//This code lets the user select the contact in system to bind to a certain group.
	?>
		<option value="<?php echo $rw['phone'] ?>"> <?php echo $rw['fname'].' '.$rw['sname'] ?></option> <?php
}
?>
</select><br/><br/>
<input type="submit" value="Add to Group" name="submit" class="sbtns">&emsp;&emsp;&emsp;
<input type="reset" name="reset" value="Cancel" class="sbtns">
</form>
<?php
if(isset($_POST['submit'])){
	$name=$_POST['name'];
	$phone=$_POST['phone'];
	$status="NE";
	$er="N/A";
	$ec="n/a";
	//This code executes the grouping of the contact to a group as well as updating the status of the group if it were empty.
	if($name ==$er || $phone==$ec){
		echo "Select Group and Contact Details to accordingly";
	}
	elseif($name !=$er && $phone !=$ec){
	$iqry=mysql_query("INSERT INTO group_contacts(group_name,phone) VALUES('$name','$phone')");
	if(!$iqry) $re="Could Not Add Contact to Group";
	else {
		echo "Contact Added to Group successfully";
		mysql_query("UPDATE info_group SET status='$status' WHERE group_name='$name'");
	}
	}
}
?>
</body>
</html>